package com.kkd.kkdapp.adapter;

import java.text.DecimalFormat;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.http.entity.BillListData;
import com.kkd.kkdapp.listener.OnSingleClickListener;
import com.kkd.kkdapp.utils.DialogUtils;

public class DebtDetailsAdapter extends BaseAdapter {

	private Context context;
	private List<BillListData> billList;
	private DecimalFormat df;
	private int mPosition = -1;
	private PopupWindow tip_popupWindow;
	private int type;

	public DebtDetailsAdapter(Context context, List<BillListData> billList,
			int type) {
		this.context = context;
		this.billList = billList;
		this.type = type;
		df = new DecimalFormat("###,##0.00");
	}

	@Override
	public int getCount() {
		return billList.size();
	}

	@Override
	public Object getItem(int position) {
		return billList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHodler mViewHodler;
		if (convertView == null) {
			convertView = View.inflate(context, R.layout.biddetailsitem, null);
			mViewHodler = new ViewHodler();
			mViewHodler.term = (TextView) convertView.findViewById(R.id.term);
			mViewHodler.date_tv = (TextView) convertView
					.findViewById(R.id.date_tv);
			mViewHodler.expected_return = (TextView) convertView
					.findViewById(R.id.expected_return);
			mViewHodler.actual_recovery = (TextView) convertView
					.findViewById(R.id.actual_recovery);
			mViewHodler.repayment_img = (ImageView) convertView
					.findViewById(R.id.repayment_img);
			mViewHodler.advance_img = (ImageView) convertView
					.findViewById(R.id.advance_img);
			mViewHodler.debted_img = (ImageView) convertView
					.findViewById(R.id.debted_img);
			mViewHodler.makeup = (TextView) convertView
					.findViewById(R.id.makeup);
			mViewHodler.interest_tv = (TextView) convertView
					.findViewById(R.id.interest_tv);
			mViewHodler.interest_rl = (RelativeLayout) convertView
					.findViewById(R.id.interest_rl);
			mViewHodler.interest = (TextView) convertView
					.findViewById(R.id.interest);
			mViewHodler.top_ll = (LinearLayout) convertView
					.findViewById(R.id.top_ll);
			mViewHodler.bottom_rl = (RelativeLayout) convertView
					.findViewById(R.id.bottom_rl);
			mViewHodler.Payment_tv = (TextView) convertView
					.findViewById(R.id.Payment_tv);
			mViewHodler.actual_recovery_tv = (TextView) convertView
					.findViewById(R.id.actual_recovery_tv);
			mViewHodler.expected_return_tv = (TextView) convertView
					.findViewById(R.id.expected_return_tv);
			mViewHodler.statetip_ll = (LinearLayout) convertView
					.findViewById(R.id.statetip_ll);
			mViewHodler.tv_fenshu_statetip = (TextView) convertView
					.findViewById(R.id.tv_fenshu_statetip);
			mViewHodler.tv_jine_statetip = (TextView) convertView
					.findViewById(R.id.tv_jine_statetip);
			mViewHodler.tip_statetip = convertView
					.findViewById(R.id.tip_statetip);
			mViewHodler.viewline2 = convertView.findViewById(R.id.viewline2);
			convertView.setTag(mViewHodler);
		} else {
			mViewHodler = (ViewHodler) convertView.getTag();
		}

		BillListData mItem = billList.get(position);

		// 期限
		mViewHodler.term.setText(mItem.getBill_num() + "期");
		mViewHodler.term.setBackgroundResource(R.drawable.term_bg);

		// 回款时间
		mViewHodler.date_tv.setText(mItem.getDate());

		// 预计回款
		mViewHodler.expected_return.setText(df.format(mItem.getTotal()));

		// 实际回收
		mViewHodler.actual_recovery.setText(df.format(mItem.getIncome()));

		// 回款desc
		if (!TextUtils.isEmpty(mItem.getDesc())) {
			mViewHodler.Payment_tv.setText(mItem.getDesc());
		} else {
			mViewHodler.Payment_tv.setText("");
		}
		// 已还图标
		if (type == 1 || type == 2) {
			if (mItem.getTotal() == 0) {
				mViewHodler.repayment_img.setVisibility(View.INVISIBLE);
				mViewHodler.advance_img.setVisibility(View.INVISIBLE);
				mViewHodler.debted_img.setVisibility(View.VISIBLE);
			} else {
				mViewHodler.debted_img.setVisibility(View.INVISIBLE);
				if (mItem.getPayment_status() == 0) {
					mViewHodler.repayment_img.setVisibility(View.INVISIBLE);
					mViewHodler.advance_img.setVisibility(View.INVISIBLE);
				} else if (mItem.getPayment_status() == 1) {
					mViewHodler.repayment_img.setVisibility(View.VISIBLE);
					mViewHodler.advance_img.setVisibility(View.INVISIBLE);
				} else if (mItem.getPayment_status() == 2) {
					mPosition = position;
					mViewHodler.repayment_img.setVisibility(View.INVISIBLE);
					mViewHodler.advance_img.setVisibility(View.VISIBLE);
				}
			}
		} else {
			mViewHodler.debted_img.setVisibility(View.INVISIBLE);
			if (mItem.getPayment_status() == 0) {
				mViewHodler.repayment_img.setVisibility(View.INVISIBLE);
				mViewHodler.advance_img.setVisibility(View.INVISIBLE);
			} else if (mItem.getPayment_status() == 1) {
				mViewHodler.repayment_img.setVisibility(View.VISIBLE);
				mViewHodler.advance_img.setVisibility(View.INVISIBLE);
			} else if (mItem.getPayment_status() == 2) {
				mPosition = position;
				mViewHodler.repayment_img.setVisibility(View.INVISIBLE);
				mViewHodler.advance_img.setVisibility(View.VISIBLE);
			}
		}
		mViewHodler.top_ll.setBackgroundColor(Color.parseColor("#FFFFFF"));
		mViewHodler.bottom_rl.setBackgroundColor(Color.parseColor("#FFFFFF"));
		mViewHodler.Payment_tv.setTextColor(Color.parseColor("#505050"));
		mViewHodler.date_tv.setTextColor(Color.parseColor("#505050"));
		mViewHodler.term.setBackgroundResource(R.drawable.term_bg);
		mViewHodler.expected_return.setTextColor(Color.parseColor("#666666"));
		mViewHodler.actual_recovery.setTextColor(Color.parseColor("#666666"));
		mViewHodler.actual_recovery_tv.setText("实际回款(元)");
		mViewHodler.actual_recovery_tv
				.setTextColor(Color.parseColor("#979797"));
		mViewHodler.expected_return_tv
				.setTextColor(Color.parseColor("#979797"));
		mViewHodler.viewline2.setBackgroundColor(Color.parseColor("#FFFFFF"));
		if (mPosition != -1 && position > mPosition) {
			mViewHodler.bottom_rl.setBackgroundColor(Color
					.parseColor("#88EEEEEE"));
			mViewHodler.top_ll
					.setBackgroundColor(Color.parseColor("#88EEEEEE"));
			mViewHodler.Payment_tv.setTextColor(Color.parseColor("#C0C0C0"));
			mViewHodler.date_tv.setTextColor(Color.parseColor("#C0C0C0"));
			mViewHodler.term.setBackgroundResource(R.drawable.term_bg_up);
			mViewHodler.expected_return.setTextColor(Color
					.parseColor("#C0C0C0"));
			mViewHodler.actual_recovery.setTextColor(Color
					.parseColor("#C0C0C0"));
			mViewHodler.actual_recovery_tv.setTextColor(Color
					.parseColor("#C0C0C0"));
			mViewHodler.expected_return_tv.setTextColor(Color
					.parseColor("#C0C0C0"));
			mViewHodler.viewline2.setBackgroundColor(Color
					.parseColor("#88EEEEEE"));
		}

		if (mItem.getType() == 0) {
			mViewHodler.statetip_ll.setVisibility(View.GONE);
		} else if (mItem.getType() == 1) {
			mViewHodler.statetip_ll.setVisibility(View.VISIBLE);
			mViewHodler.tv_fenshu_statetip.setText(mItem.getTransParts() + "份");
			mViewHodler.tv_jine_statetip.setText(df.format(mItem
					.getTransAmount() * 0.01) + "元");
			mViewHodler.tip_statetip
					.setOnClickListener(new OnSingleClickListener() {

						@Override
						public void onSingleClick(View v) {
							if (tip_popupWindow != null) {
								tip_popupWindow.dismiss();
								tip_popupWindow = null;
							}
							tip_popupWindow = DialogUtils.showTipPopupWindow(
									context, v, "实收金额已扣除服务费");
						}
					});
		}

		if (TextUtils.isEmpty(mItem.getIncome_compensate())) {
			mViewHodler.makeup.setVisibility(View.GONE);
		} else {
			mViewHodler.makeup.setVisibility(View.VISIBLE);
			mViewHodler.makeup.setText(mItem.getIncome_compensate());
		}
		// 计息
		if (TextUtils.isEmpty(mItem.getInterest_days())) {
			mViewHodler.interest_rl.setVisibility(View.GONE);
		} else {
			mViewHodler.interest_rl.setVisibility(View.VISIBLE);
			mViewHodler.interest.setText(mItem.getInterest_days());
		}
		return convertView;
	}

	class ViewHodler {

		TextView term, date_tv, expected_return, actual_recovery, Payment_tv,
				actual_recovery_tv, expected_return_tv;
		ImageView repayment_img, advance_img, debted_img;
		TextView makeup, interest_tv, interest;
		RelativeLayout interest_rl, bottom_rl;
		LinearLayout top_ll, statetip_ll;
		TextView tv_fenshu_statetip, tv_jine_statetip;
		View tip_statetip, viewline2;
	}
}
